//
//  MyViewController.m
//  SunXi
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "MyViewController.h"
#import <TencentOpenAPI/QQApiInterface.h>
@interface MyViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSMutableArray *arr;
@property(nonatomic, retain) NSMutableArray *tuArr;

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    self.arr = [NSMutableArray arrayWithObjects:@"收藏", @"清除缓存", @"切换模式", @"瞬息心情",nil];
    self.tuArr = [NSMutableArray arrayWithObjects:@"shou.png", @"qing.png", @"day.png",   @"biaoqing.png", nil];
    
    UIImageView *imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"drawBackPic.jpg"]];
    [self.view addSubview:imageview];
    [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(SV).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT  / 5, WIDTH, HEIGHT / 20 * 9) style:UITableViewStylePlain];
    [SV addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.5];
    // 横杆
    self.tableView.separatorStyle = NO;
    // 回弹
    self.tableView.scrollEnabled = NO;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return HEIGHT / 2 / self.arr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = self.arr[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:self.tuArr[indexPath.row]];
    if (indexPath.row == 1) {
        
        cell.textLabel.text = self.arr[indexPath.row];
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *sandBoxPath = [sandBox lastObject];
        CGFloat size = [self folderSizeAtPath:sandBoxPath];
        cell.textLabel.text = [NSString stringWithFormat:@"清理缓存(%.2fM)", size];
        cell.selectionStyle = NO;
        
    }
    //cell选中的颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row ==0) {
        CollectionViewController *collVC = [[CollectionViewController alloc] init];
        [self.navigationController pushViewController:collVC animated:YES];
    }
        
    if (indexPath.row == 1) {
        
        // 确定手机系统版本
        NSString *phoneVersion = [[UIDevice currentDevice] systemVersion];
        NSLog(@"手机系统版本: %@", phoneVersion);
        NSLog(@"%ld", (long)phoneVersion.integerValue);
        
        if (phoneVersion.integerValue >= 8) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要清除缓存吗?" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
                NSString *sandBoxPath = [sandBox lastObject];
                [self clearCache:sandBoxPath];
                UITableView *tblView = (UITableView *)[self.view viewWithTag:1000];
                [tblView reloadData];
                
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                
            }];
            
            [alert addAction:action1];
            [alert addAction:action2];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        } else {
            
            UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要清除缓存吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alertV show];
        }
    }    
    if (indexPath.row == 3) {
        
        MoodViewController *moodVC = [[MoodViewController alloc] init];
        [self.navigationController pushViewController:moodVC animated:YES];
    }
    
    
    if (indexPath.row == 2) {
        
        [self nightFalls];
        
    }
    
    
}

- (void)nightFalls {
    NSLog(@"oo");
    if ([DKNightVersionManager currentThemeVersion] == DKThemeVersionNight) {
        [DKNightVersionManager dawnComing];
    } else {
        [DKNightVersionManager nightFalling];
    }
}
// 清除缓存
- (void)clearCache:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
            [self.tableView reloadData];
        }
    }
}

// 计算单个文件大小
- (CGFloat)fileSizeAtPath:(NSString *)path {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size / 1024.0 / 1024.0;
    }
    return 0;
}

// 计算目录大小
- (CGFloat)folderSizeAtPath:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:absolutePath];
        }
    }
    
    return folderSize;
}

// 点击alert上的按钮执行的方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *sandBoxPath = [sandBox lastObject];
        [self clearCache:sandBoxPath];
        UITableView *tblView = (UITableView *)[self.view viewWithTag:1000];
        [tblView reloadData];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
